#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QDebug>
#include <QThread>

#include "RandomDistribution.h"
#include "Parser.h"
#include "Observation.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    particleFilter(new ParticleFilter())
{
    ui->setupUi(this);

    QThread* thread = new QThread();

    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

    particleFilter->moveToThread(thread);

    connect(particleFilter, SIGNAL(resultReady(double,double,double,double)), this, SLOT(resultReady(double,double,double,double)), Qt::DirectConnection);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_startButton_clicked()
{
    if (ui->inputText->toPlainText().isEmpty())
        return;

    ui->outputText->clear();

    Parser parser(ui->inputText->toPlainText());

    QVector<Observation> observations;

    QStringList distancesToA = parser.getColumn(2);
    QStringList distancesToB = parser.getColumn(3);

    for (int i = 0; i < distancesToA.count(); i++) {
        observations.append(Observation(distancesToA[i], distancesToB[i]));
    }

    particleFilter->start(observations, ui->sampleSizeEdit->text().toInt());

}

void MainWindow::resultReady(double meanX, double meanY, double varianceX, double varianceY)
{

    QString out(QString::number(meanX) + " & " + QString::number(meanY) + " & " + QString::number(varianceX) + " & " + QString::number(varianceY) + " \\\\");
    ui->outputText->append(out);
    ui->outputText->repaint();
}
